package cn.lanqiao.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.lanqiao.entity.ApiResult;
import cn.lanqiao.entity.Exam_manage;
import cn.lanqiao.entity.Near;
import cn.lanqiao.entity.Pages;
import cn.lanqiao.util.ApiResultHandler;

@CrossOrigin
@RequestMapping("/api/exam")
@RestController
public class Exam_manageController {

	@Autowired
	private cn.lanqiao.service.Exam_manageService exam_manageService;
	
	//分页
	@GetMapping(value="/exampage")
	public Pages<Exam_manage> pager(
			@RequestParam(value="pageindex") int pageindex,
			@RequestParam(value="pagesize") int pagesize,
			@RequestParam(value="searchkey",required=false,defaultValue = "") String searchkey){
		return exam_manageService.ExamPage(pageindex, pagesize, searchkey);
	}
	
	//获取单条数据
	@ GetMapping("/get/{examCode}")
	public Map<String,Object> get(@PathVariable (value="examCode") Integer examCode){
		Map<String,Object>  map = new HashMap<String,Object>();
		Exam_manage exam = exam_manageService.getExamByCode(examCode);
		if(exam!=null) {
			map.put("data",exam);
			map.put("status", 200);
			map.put("statusText","获取数据成功 ");
		}else {
			map.put("data",null);
			map.put("status", 0);
			map.put("statusText","获取数据失败 ");
		}
		return map;
	}
	
	//删除
	@DeleteMapping("/delete/{examCode}")
	public Map<String,Object> delete(@PathVariable (value="examCode") Integer examCode){
		Map<String,Object>  map = new HashMap<String,Object>();
		int result = exam_manageService.deleteExam(examCode);
		if(result>0) {
			map.put("status", 200);
			map.put("statusText", "删除成功");
		}else{
			map.put("status", 0);
			map.put("statusText", "删除失败");
		}
		return map;
	}
	
	//更新
	@PutMapping("/update")
	public Map<String , Object > update( @RequestBody  Exam_manage exam ){
		int result = exam_manageService.updateExam(exam);
		Map<String,Object>  map = new HashMap<String,Object>();
		if( result >0 ) {
			map.put("status", 200);
			map.put("statusText", "更新成功");
		} else {
			map.put("status", 0);
			map.put("statusText", "更新失败");
		}
		return map;
	}
	
	//添加
	@PostMapping("/insert")
	public Map<String,Object> insert(@RequestBody Exam_manage exam){
		Map<String,Object>  map = new HashMap<String,Object>();
		int result = exam_manageService.insertExam(exam);
		if(result>0) {
			map.put("status", 200);
			map.put("statusText", "添加成功");
		}else{
			map.put("status", 0);
			map.put("statusText", "添加失败");
		}
		return map;
	}	
	//查询最近考试
	@GetMapping("/nearexam")
	public List<Near> Students ( @RequestParam(value="day",required=false,defaultValue = "10") int day ){
		return exam_manageService.nearAllExam(day);
	}
	//查询考试信息
	@GetMapping("/exams")
    public ApiResult findAll(){
//        System.out.println("不分页查询所有试卷");
        ApiResult apiResult;
        apiResult = ApiResultHandler.buildApiResult(200, "请求成功！", exam_manageService.findAll());
        return apiResult;
    }
   //分页查询考试信息
    @GetMapping("/exams/{page}/{size}")
    public ApiResult findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
//        System.out.println("分页查询所有试卷");
        ApiResult apiResult;
        Page<Exam_manage> examManage = new Page<>(page,size);
        IPage<Exam_manage> all = exam_manageService.findAll(examManage);
        apiResult = ApiResultHandler.buildApiResult(200, "请求成功！", all);
        return apiResult;
    }
   //根据试卷查询信息
    @GetMapping("/exam/{source}")
    public ApiResult findById(@PathVariable("source") String source){
//        System.out.println("根据ID查找");
        Exam_manage res = exam_manageService.findBySource(source);
        if(res == null) {
            return ApiResultHandler.buildApiResult(10000,"考试信息不存在",null);
        }
        return ApiResultHandler.buildApiResult(200,"请求成功！",res);
    }

   
   //根据试卷id查询信息
    @GetMapping("/examManagePaperId")
    public ApiResult findOnlyPaperId() {
        Exam_manage res = exam_manageService.findOnlyPaperId();
        if (res != null) {
            return ApiResultHandler.buildApiResult(200,"请求成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"请求失败",res);
    }
	
}
